package com.atguigu.spzx.manager.task;

import cn.hutool.core.date.DateUtil;
import com.atguigu.spzx.manager.mapper.OrderInfoMapper;
import com.atguigu.spzx.manager.mapper.OrderStatisticsMapper;
import com.atguigu.spzx.model.entity.order.OrderStatistics;
import jakarta.annotation.Resource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;


/**
 * ClassName: OrderStatisticTask
 * Package:
 * Description:
 *
 * @Author:xiaohei
 * @Create 2024/5/20 23:06
 * Version 1.0
 */
@Component
public class OrderStatisticTask {

    //测试定时任务
//    @Scheduled(cron = "0/5 * * * * ?")
//    public void sayHello(){
//        System.out.println(new Date().toInstant());
//    }

    //每天凌晨两点，查询前一天统计数据，把统计之后数据添加到统计结果表里面去   订单数据统计、

    @Resource
    private OrderInfoMapper orderInfoMapper;
    @Resource
    private OrderStatisticsMapper orderStatisticsMapper;

    @Scheduled(cron = "0 0 2 * * ?")
    public void orderTotalAmountStatistics(){
        //1 获取前一天的日期
        String createDate = DateUtil.offsetDay(new Date(), -1).toString("yyyy-MM-dd");

        //2 根据前一天的日期进行统计功能 （分组操作）
        //统计前一天的交易金额
        OrderStatistics orderStatistics = orderInfoMapper.selectStatistics(createDate);

        //3 统计之后的数据添加到结果表
        if(orderStatistics != null){
            orderStatisticsMapper.insert(orderStatistics);
        }
    }

}
